<?php
class AppModel extends Model{
	
	
	function RHgetClientIP()
	{
		App::import('Component', 'RequestHandler');
		$rh = new RequestHandlerComponent();
		return $rh->getClientIP();
	}
	
	function updateStatus($mode)
	{
		$data = array();
		if($mode === 'resign'){$status = 'resign';}
		elseif($mode === 'activate'){$status = 'active';}
		elseif($mode === 'disactivate'){$status = 'disable';}
		
		$data['ip'] = $this->RHgetClientIP();
		$data['status'] = $status;
		$data['modified'] = null;
		return $this->save(array($this->name => $data), true, array('ip', 'status', 'modified'));
	}
	
	//テーブル名のゲッタ
	function getTableName()
	{
		return $this->name;
	}
	
	//arrayの中にあるかどうか
	function isInArray($data, $array)
	{
		return (in_array(current($data), $array));
	}
	
	//重複チェックのバリデート
	function checkDuplicate($data, $addconds = array())
	{
		$cond = array(key($data) => '= '.current($data));
		if(($id = $this->getID()) !== false){
			$cond[$this->primaryKey] = '!= '.$id;
		}
		foreach($addconds as $key => $value){
			$cond[$key] = $value;
		}
		return ($this->findCount($cond, -1) === 0);
	}
	
	//正の整数かどうか
	function isPlusNumber($data)
	{
		$pattern = "`^[1-9][0-9]*$`";
		return (preg_match($pattern, current($data)) === 1);
	}
	
	//boolかどうかのバリデート
	function isBool($data)
	{
		return (is_bool(current($data)));
	}
	
	//改行n以下のバリデート
	function kaigyoLessThanN($data, $numKaigyo)
	{
		$bool = false;
		
		$pattern = "`\r\n|\n|\r`u";
		$count = preg_match_all($pattern, current($data), $array);
		if($count === false){
		}elseif($count <= $numKaigyo){
			$bool = true;
		}
		return $bool;
	}
	
    var $hasMany_full = array();
    var $hasAndBelongsToMany_full = array();
    var $belongsTo_full = array();
    
    function bindFully() {
        $this->bindModel(array('hasMany' => $this->hasMany_full,
                               'hasAndBelongsToMany' => $this->hasAndBelongsToMany_full,
                               'belongsTo' => $this->belongsTo_full));
    }
    
    function unbindFully() {
        $unbind = array();
        foreach ($this->belongsTo as $model=>$info) {
            $unbind['belongsTo'][] = $model;
        }
        foreach ($this->hasOne as $model=>$info) {
            $unbind['hasOne'][] = $model;
        }
        foreach ($this->hasMany as $model=>$info) {
            $unbind['hasMany'][] = $model;
        }
        foreach ($this->hasAndBelongsToMany as $model=>$info) {
            $unbind['hasAndBelongsToMany'][] = $model;
        }
        $this->unbindModel($unbind);
    }	


}
?>